﻿using System;
using System.IO;
using BlueLotus7.MetaWeblogUtil.CnBlogs;
using BlueLotus7.MetaWeblogUtil.CnBlogs.Contract;
using BlueLotus7.MetaWeblogUtil.Contract;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTest
{
    [TestClass]
    public class CnBlogs
    {
        const string title = "libarary test";
        const string content = "libarary test content";
        CnBlogsAgent agent = new CnBlogsAgent("ACCOUT", "PASSWORD", "rynnwang");
        string postId = string.Empty;

        [TestMethod]
        public void AddPost()
        {
            CnBlogsPost post = new CnBlogsPost();
            post.title = title;
            post.description = content;
            post.categories = new string[] { "category test" };
            postId = agent.AddPost(post);

            Assert.IsTrue(!string.IsNullOrWhiteSpace(postId));
        }

        [TestMethod]
        public void GetAndUpdatePost()
        {
            CnBlogsPost post = agent.GetPost(postId);
            Assert.IsTrue(post != null && post.title == title);

            post.description = content + " updated";
            agent.UpdatePost(post);

            post = agent.GetPost(postId);
            Assert.IsTrue(post != null && post.description.EndsWith("updated"));
        }

        [TestMethod]
        public void NewMediaObject()
        {
            string fileName = "test.jpg";
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);

            byte[] binaries = File.ReadAllBytes(path);
            FileData fileData = new FileData();
            fileData.name = fileName;
            fileData.bits = (binaries);
            UrlData info = agent.NewMediaObject(postId, fileData);

            Assert.IsTrue(!string.IsNullOrWhiteSpace(info.url));
        }

        [TestMethod]
        public void GetRecentPosts()
        {
            var posts = agent.GetRecentPosts(10);
            Assert.IsTrue(posts != null && posts.Length > 0);
        }

        [TestMethod]
        public void GetCategories()
        {
            var categories = agent.GetCategories();
            Assert.IsTrue(categories != null && categories.Length > 0);
        }

        [TestMethod]
        public void DeletePost()
        {
            agent.DeletePost(postId);
        }


    }
}
